package com.forezp.web;

import com.forezp.entity.DataSource;
import com.forezp.service.DataSourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * springboot-jdbc-DataSourceController
 *
 * @author <a href="2428922347@qq.com">Zhu mingming</a>
 * @info
 * @since 2018/3/16 14:16
 */
@Controller
@RequestMapping
public class DataSourceController {

    @Autowired
    private DataSourceService dataSourceService;

    @RequestMapping("/dataSource")
    public String dataSource(Model model, Integer dataSourceId) {
        List<DataSource> dataSourceList = dataSourceService.getAll();
        model.addAttribute("datasource", dataSourceList);
        if (dataSourceId != null) {
            DataSource dataSource = dataSourceService.findById(dataSourceId);
            model.addAttribute("targetDatasource", dataSource);
        }
        return "dataSource";
    }

    @RequestMapping("/dataSource/add")
    public String addSource(DataSource dataSource) {
        dataSourceService.save(dataSource);
        return "forward:/dataSource";
    }

}
